#!/usr/bin/env python
# -*- coding: UTF-8 -*-

__version__ = "0.3.0"
__date__ = "2011-04-11"


from lightdata import reader,get_utc,get_counts,get_temperature
from optparse import OptionParser
from pylab import *


def main():
    parser = OptionParser()
    parser.add_option("-i","--filename-in", dest="filename_in", default='', metavar="file",
                                      help="csv filename in")
    parser.add_option("-n","--station", dest="station", default="", metavar="id",
                                      help="station id")
    parser.add_option("-c","--show-counts", dest="show_counts", default=False, action="store_true",
                                      help="show counts")
    parser.add_option("-t","--show-temperature", dest="show_temperature", default=False, action="store_true",
                                      help="show temperature")
    parser.add_option("-s","--sensor", dest="sensor", default="", metavar="snr",
                                      help="sensor serial number")
    parser.add_option("--lat", dest="latitude", default="", metavar="latitude",
                                      help="set latitude")
    parser.add_option( "--lon",dest="longitude", default="", metavar="longitude",
                                      help="set longitude")
    parser.add_option("--step", dest="step", default="1", metavar="step",
                                      help="step value for reading data [%default]")
                                      
    parser.add_option("-v", "--verbose",action="store_true", dest="verbose", default=False,
                                      help="be verbose")
    parser.add_option("-d","--debug", dest="debug", default=False, action="store_true",
                                      help="enable debug")
    (options, args) = parser.parse_args()
    
    if not options.filename_in or (not options.show_counts and not options.show_temperature):
        parser.print_help()
        exit(-1) 
        
    data = reader(filename_in=options.filename_in,\
                  station = options.station,\
                  sensor = options.sensor,\
                  latitude = options.latitude,\
                  longitude = options.longitude,\
                  step = options.step,\
                  verbose=options.verbose,\
                  debug=options.debug)
                  
    figure(1)  
    xticks(rotation=25)
    
    utc= array(get_utc(data))
    
    if options.show_counts:
        counts= array(get_counts(data))
        plot_date(date2num(utc),counts,fmt='-')
        
    elif options.show_temperature: 
        temperature= array(get_temperature(data))
        plot_date(date2num(utc),temperature,fmt='-')
        
    show()

if __name__ == "__main__":
    main()


